package com.lpit.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lpit.dto.UserLoginDto;
import com.lpit.mapper.UserMapper;
import com.lpit.pojo.User;
import com.lpit.result.ResponseResult;
import com.lpit.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    UserMapper userMapper;

//    登录
    @Override
    public ResponseResult< List<User> > login(UserLoginDto userLoginDto) {
        LambdaQueryWrapper<User> adminLambdaQueryWrapper=new LambdaQueryWrapper<>();
        adminLambdaQueryWrapper.eq(true,User::getName,userLoginDto.getName()).eq(true,User::getPassword,userLoginDto.getPassword());
        List<User> users = userMapper.selectList(adminLambdaQueryWrapper);
//        这里是有查出账号密码
        return new ResponseResult<>(200,users,"成功");
    }



//    注册
    @Override
    public ResponseResult<User> Add(UserLoginDto userLoginDto) {
        if(userLoginDto.getName()==null ||userLoginDto.getPassword()==null){
//            输入密码不能为空
            return null;
        }

        //将用户新添的账号到数据库内进行对比，存在就新添用户已存在，不存在就新添用户成功
        String userName=userMapper.selectName(userLoginDto.getName());
        if(userName!=null){
//            注册的账号不为空说明这个账号在
            return null;
        }

//        密码加密
//        String password = DigestUtils.md5DigestAsHex(userLoginDto.getPassword().getBytes());
        userMapper.insertSelective(userLoginDto);
        return new ResponseResult<User>(200,null,"添加成功");


    }
}
